﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BB.Apostas.Filters;
using BB.Apostas.Models;
using BB.Processes.Services.Logging;
using BB.Processes.Services.Processes;
using Newtonsoft.Json;
using WebMatrix.WebData;
using System.Globalization;
using PagedList;
using System.Data.Entity;

namespace BB.Apostas.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class AdminController : Controller
    {
        private ApostasBalasDbContext db = new ApostasBalasDbContext();

        public ActionResult Index()
        {
            return View();
        }

        #region Processes

        public void Process(string type)
        {
            switch (type)
            {
                case "all":
                    BB.Processes.Services.Processes.Apostas.ExecuteRss();
                    BB.Processes.Services.Processes.Apostas.ExecuteApostas();
                    BB.Processes.Services.Processes.Apostas.ExecuteJogos();
                    BB.Processes.Services.Processes.Apostas.ExecuteJornadas();
                    BB.Processes.Services.Processes.Apostas.ExecutarNotificacao();
                    break;
                case "rss":
                    BB.Processes.Services.Processes.Apostas.ExecuteRss();
                    break;
                case "apostas":
                    BB.Processes.Services.Processes.Apostas.ExecuteApostas();
                    break;
                case "jogos":
                    BB.Processes.Services.Processes.Apostas.ExecuteJogos();
                    break;
                case "jornadas":
                    BB.Processes.Services.Processes.Apostas.ExecuteJornadas();
                    break;
                case "notificacao":
                    BB.Processes.Services.Processes.Apostas.ExecutarNotificacao();
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region Competicoes

        public ActionResult IndexCompeticao()
        {
            return View(db.Competicoes.ToList());
        }

        public ActionResult CreateCompeticao()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateCompeticao(Competicao competicao)
        {
            if (ModelState.IsValid)
            {

                db.Competicoes.Add(competicao);
                db.SaveChanges();
                return RedirectToAction("IndexCompeticao");
            }

            return View(competicao);
        }

        public ActionResult EditCompeticao(int id = 0)
        {
            Competicao competicao = db.Competicoes.Find(id);
            if (competicao == null)
            {
                return HttpNotFound();
            }
            return View(competicao);
        }

        [HttpPost]
        public ActionResult EditCompeticao(Competicao competicao)
        {
            if (ModelState.IsValid)
            {
                db.Entry(competicao).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexCompeticao");
            }
            return View(competicao);
        }

        public ActionResult DeleteCompeticao(int id = 0)
        {
            Competicao competicao = db.Competicoes.Find(id);
            if (competicao == null)
            {
                return HttpNotFound();
            }
            return View(competicao);
        }

        [HttpPost, ActionName("DeleteCompeticao")]
        public ActionResult DeleteCompeticaoConfirmed(int id)
        {
            Competicao competicao = db.Competicoes.Find(id);
            db.Competicoes.Remove(competicao);
            db.SaveChanges();
            return RedirectToAction("IndexCompeticao");
        }

        #endregion

        #region Jornadas

        public ActionResult IndexJornada()
        {
            return View(db.Jornadas.OrderBy(j => j.CompeticaoId).ToList());
        }

        public ActionResult CreateJornada()
        {
            ViewBag.CompeticaoId = new SelectList(db.Competicoes.OrderBy(r => r.Name), "Id", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateJornada(Jornada jornada)
        {
            if (ModelState.IsValid)
            {

                db.Jornadas.Add(jornada);
                db.SaveChanges();
                return RedirectToAction("IndexJornada");
            }

            return View(jornada);
        }

        public ActionResult EditJornada(int id = 0)
        {
            Jornada jornada = db.Jornadas.Find(id);
            if (jornada == null)
            {
                return HttpNotFound();
            }
            ViewBag.CompeticaoId = new SelectList(db.Competicoes.OrderBy(r => r.Name), "Id", "Name", jornada.CompeticaoId);
            return View(jornada);
        }

        [HttpPost]
        public ActionResult EditJornada(Jornada jornada)
        {
            if (ModelState.IsValid)
            {
                db.Entry(jornada).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexJornada");
            }
            return View(jornada);
        }

        public ActionResult DeleteJornada(int id = 0)
        {
            Jornada jornada = db.Jornadas.Find(id);
            if (jornada == null)
            {
                return HttpNotFound();
            }
            return View(jornada);
        }

        [HttpPost, ActionName("DeleteJornada")]
        public ActionResult DeleteJornadaConfirmed(int id)
        {
            Jornada jornada = db.Jornadas.Find(id);
            db.Jornadas.Remove(jornada);
            db.SaveChanges();
            return RedirectToAction("IndexJornada");
        }

        #endregion

        #region Jogos

        public ActionResult IndexJogo(int? page)
        {
            ViewBag.SectionIndex = "IndexJogo";
            int pageNumber = (page ?? 1);
            return View(db.Jogos.OrderBy(j => j.CompeticaoId).ToPagedList(pageNumber, 12));
        }

        public ActionResult CreateJogo()
        {
            ViewBag.CompeticaoId = new SelectList(db.Competicoes.OrderBy(r => r.Name), "Id", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateJogo(Jogo jogo)
        {
            if (ModelState.IsValid)
            {
                db.Jogos.Add(jogo);
                db.SaveChanges();
                return RedirectToAction("IndexJogo");
            }
            ViewBag.CompeticaoId = new SelectList(db.Competicoes.OrderBy(r => r.Name), "Id", "Name", string.Empty);
            return View(jogo);
        }

        public ActionResult EditJogo(int id = 0)
        {
            Jogo jogo = db.Jogos.Find(id);
            if (jogo == null)
            {
                return HttpNotFound();
            }
            ViewBag.CompeticaoId = new SelectList(db.Competicoes.OrderBy(r => r.Name), "Id", "Name", jogo.CompeticaoId);
            ViewBag.JornadaId = new SelectList(db.Jornadas.OrderBy(r => r.Id), "Id", "Name", jogo.JornadaId);
            return View(jogo);
        }

        [HttpPost]
        public ActionResult EditJogo(Jogo jogo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(jogo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexJogo");
            }
            return View(jogo);
        }

        public ActionResult DeleteJogo(int id = 0)
        {
            Jogo jogo = db.Jogos.Find(id);
            if (jogo == null)
            {
                return HttpNotFound();
            }
            return View(jogo);
        }

        [HttpPost, ActionName("DeleteJogo")]
        public ActionResult DeleteJogoConfirmed(int id)
        {
            Jogo jogo = db.Jogos.Find(id);
            db.Jogos.Remove(jogo);
            db.SaveChanges();
            return RedirectToAction("IndexJogo");
        }      

        public class JornadaSelect
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public JsonResult GetJornadas(int idcompeticao = 0)
        {
            try
            {
                var jornadas = db.Jornadas
                                   .Where(c => c.CompeticaoId == idcompeticao)
                                   .Select(j => new JornadaSelect { Id = j.Id, Name = j.Name })
                                   .OrderBy(c => c.Id)
                                   .ToList();
                return Json(jornadas, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Utilizadores

        public ActionResult IndexUtilizador()
        {
            return View(db.UserProfiles.ToList());
        }

        public ActionResult CreateUtilizador()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateUtilizador(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email });
                return RedirectToAction("IndexUtilizador");
            }
            return View(model);
        }

        public ActionResult EditUtilizador(int id = 0)
        {
            UserProfile user = db.UserProfiles.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        [HttpPost]
        public ActionResult EditUtilizador(UserProfile user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexUtilizador");
            }
            return View(user);
        }

        public ActionResult DeleteUtilizador(int id = 0)
        {
            UserProfile user = db.UserProfiles.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        [HttpPost, ActionName("DeleteUtilizador")]
        public ActionResult DeleteUtilizadorConfirmed(int id)
        {
            var user = db.UserProfiles.Find(id);
            foreach (var role in Roles.GetRolesForUser(user.UserName))
                Roles.RemoveUserFromRole(user.UserName, role); 
            ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(user.UserName);
            ((SimpleMembershipProvider)Membership.Provider).DeleteUser(user.UserName, true);
            return RedirectToAction("IndexUtilizador");
        }

        #endregion

        #region Noticias

        public ActionResult IndexNoticia()
        {
            return View(db.Noticias.OrderByDescending(n => n.Date).ToList());
        }

        public ActionResult CreateNoticia()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNoticia(Noticia noticia)
        {
            noticia.Date = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Noticias.Add(noticia);
                db.SaveChanges();
                return RedirectToAction("IndexNoticia");
            }
            return View(noticia);
        }

        public ActionResult EditNoticia(int id = 0)
        {
            Noticia noticia = db.Noticias.Find(id);
            if (noticia == null)
            {
                return HttpNotFound();
            }
            return View(noticia);
        }

        [HttpPost]
        public ActionResult EditNoticia(Noticia noticia)
        {
            if (ModelState.IsValid)
            {
                db.Entry(noticia).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexNoticia");
            }
            return View(noticia);
        }

        public ActionResult DeleteNoticia(int id = 0)
        {
            Noticia noticia = db.Noticias.Find(id);
            if (noticia == null)
            {
                return HttpNotFound();
            }
            return View(noticia);
        }

        [HttpPost, ActionName("DeleteNoticia")]
        public ActionResult DeleteNoticiaConfirmed(int id)
        {
            Noticia noticia = db.Noticias.Find(id);
            db.Noticias.Remove(noticia);
            db.SaveChanges();
            return RedirectToAction("IndexNoticia");
        }

        #endregion

        #region Reporte

        public ActionResult IndexReporte()
        {
            return View(db.Reportes.OrderByDescending(n => n.CreatedDate).ToList());
        }

        public ActionResult EditReporte(int id = 0)
        {
            Reporte reporte = db.Reportes.Find(id);
            if (reporte == null)
            {
                return HttpNotFound();
            }
            return View(reporte);
        }

        [HttpPost]
        public ActionResult EditReporte(Reporte reporte)
        {
            if (ModelState.IsValid)
            {
                db.Entry(reporte).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexReporte");
            }
            return View(reporte);
        }

        #endregion
    }
}
